﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.Hosting;
using UDS.Components;

namespace UDS.Entity
{
    public enum Sex
    {
        Unspecified = -1,
        Female = 0,
        Male = 1,
    }

    /// <summary>
    /// Describes the information about a staff (UDS_Staff table).
    /// </summary>
    public class StaffInfo
    {
        public int ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string Password
        {
            get;
            set;
        }

        public string RealName
        {
            get;
            set;
        }

        public Sex Sex
        {
            get;
            set;
        }

        public string SexStr
        {
            get { return Sex == Sex.Male ? "男" : (Sex == Sex.Female ? "女" : "未指定"); }
        }

        public string Email
        {
            get;
            set;
        }

        public string Status
        {
            get;
            set;
        }

        public DateTime TimeRegistered
        {
            get;
            set;
        }

        public bool Dimission
        {
            get;
            set;
        }

        public string Phone
        {
            get;
            set;
        }

        public string Mobile
        {
            get;
            set;
        }

        public DateTime Birthday
        {
            get;
            set;
        }

        /// <summary>
        /// Get the display text of the staff's age
        /// </summary>
        public string AgeStr
        {
            get
            {
                if (Birthday == DateTime.MinValue || Birthday >= DateTime.Now)
                {
                    return "-";
                }

                int age = DateTime.Now.Year - Birthday.Year;
                if (DateTime.Now.DayOfYear >= Birthday.DayOfYear)
                {
                    age++;
                }
                return age.ToString();
            }
        }

        public int Style
        {
            get;
            set;
        }

        public bool IsNeedKey
        {
            get;
            set;
        }

        /// <summary>
        /// 职级
        /// </summary>
        public int Caste
        {
            get;
            set;
        }

        public int PositionID
        {
            get;
            set;
        }

        /// <summary>
        /// Get the name of the position.
        /// </summary>
        public string PositionName
        {
            get;
            private set;
        }

        internal static StaffInfo ReadStaffInfo(IDataReader dr)
        {
            return ReadStaffInfo(dr, true);
        }

        internal static StaffInfo ReadStaffInfo(IDataReader dr, bool readPosition)
        {
            StaffInfo s = new StaffInfo();

            s.ID = (int)dr["Staff_ID"];
            s.Name = (string)dr["Staff_Name"];
            s.RealName = Convert.ToString(dr["RealName"]);
            s.Password = Convert.ToString(dr["Password"]);

            object sex = dr["Sex"];
            if (sex == null || sex == DBNull.Value)
            {
                s.Sex = Sex.Unspecified;
            }
            else
            {
                s.Sex = ((bool)sex) ? Sex.Male : Sex.Female;
            }

            s.Email = Convert.ToString(dr["Email"]);
            s.Status = Convert.ToString(dr["Status"]);
            s.TimeRegistered = Utility.ReadSQLDate(dr["RegistedDate"]);
            s.Dimission = Utility.ReadBoolean(dr["Dimission"], false);
            s.Phone = Convert.ToString(dr["Phone"]);
            s.Mobile = Convert.ToString(dr["Mobile"]);
            s.Birthday = Utility.ReadSQLDate(dr["Birthday"]);
            s.Style = Utility.ReadInt32(dr["Style"], 0);
            if (s.Birthday != DateTime.MinValue)
            {
                s.Birthday = s.Birthday.Date;
            }
            s.IsNeedKey = Utility.ReadBoolean(dr["IsNeedKey"], false);
            s.Caste = Utility.ReadInt32(dr["Caste"], 0);

            if (readPosition)
            {
                s.PositionID = (int)dr["Position_ID"];
                s.PositionName = Convert.ToString(dr["Position_Name"]);
            }

            return s;
        }
    }
}
